package com.example.Lezedata.ui.annPage;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProvider;

import com.example.Lezedata.R;
import com.example.Lezedata.dataManager.GBUserManager;
import com.example.Lezedata.net.GBError;
import com.example.Lezedata.net.GBNetRequest;
import com.example.Lezedata.net.ServerResponseObserver;
import com.example.Lezedata.ui.activity.common.WebVc;
import com.example.Lezedata.ui.dataPage.subVc.ChequeDetailList;
import com.example.Lezedata.ui.fragment.BaseFragment;
import com.example.Lezedata.util.GBUtil;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;

import bid.SystemAnnouncement;
import common.Common;

public class AnnListFragment extends BaseFragment {

    SystemAnnouncement.AnnounceType announceType=SystemAnnouncement.AnnounceType.ANCT_ANNOUNCE;
    private AnnListViewModel mViewModel;
    public static AnnListFragment newInstance() {
        return new AnnListFragment();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_ann_list, container, false);
    }
    public void setAnnounceType( SystemAnnouncement.AnnounceType type ){
        announceType=type ;
    }
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mViewModel = new ViewModelProvider(this).get(AnnListViewModel.class);
        // TODO: Use the ViewModel


    }

    private int pageStart = 0;
    private int pageSize = 20;
    private SmartRefreshLayout refreshLayout;
    private ListView listView;
    private ArrayList<SystemAnnouncement.TBAnnouncement> announcements = new ArrayList<>();
    @Override
    public void onViewCreated(@NonNull @NotNull View view, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        listView = findViewById(R.id.listView);
        refreshLayout = findViewById(R.id.refreshLayout);
        refreshLayout.setEnableRefresh(false);
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull @NotNull RefreshLayout refreshLayout) {
                loadData();
            }
        });
        listView.setAdapter(adapter);
//        hudShow();


        loadData();

    }



    void loadData(){
        Common.PageInfo pageInfo = Common.PageInfo.newBuilder().setPageStart(pageStart).setPageSize(pageSize).build();
        SystemAnnouncement.TBNoticeCountRequest request = SystemAnnouncement.TBNoticeCountRequest.newBuilder().setType(announceType).setPageInfo(pageInfo).build();
        GBNetRequest.stub().getAnnouncementListForApp(request, ServerResponseObserver.observer(new ServerResponseObserver.requestListener<SystemAnnouncement.TBAnnouncementListResponse>() {
            @Override
            public void complete(SystemAnnouncement.TBAnnouncementListResponse value, GBError error) {
                refreshLayout.finishLoadMore();
                hudDismiss();
                if (error == null){
                    if (pageStart == 0){
                        announcements.clear();
                    }
                    announcements.addAll(value.getNoticesList());
                    if (value.getTotal() >= pageSize){
                        pageStart++;
                    }
                    refreshLayout.setEnableLoadMore(value.getNoticesCount() >= pageSize);
                    adapter.notifyDataSetChanged();
                }
            }
        }));
    }

    BaseAdapter adapter = new BaseAdapter() {
        @Override
        public int getCount() {
            return announcements.size();
        }

        @Override
        public SystemAnnouncement.TBAnnouncement  getItem(int position) {
            return announcements.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            if (convertView == null){
                convertView = AnnListCell.newInstance(getContext(),listView);
            }
            AnnListCell cell = (AnnListCell)convertView;
            cell.setPool(getItem(position));

            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    tapToAnnDetail(getItem(position));
                }
            });

            return convertView;
        }
        private void tapToAnnDetail(SystemAnnouncement.TBAnnouncement announcement){
            Intent intent = new Intent(getContext(), WebVc.class);
            intent.putExtra("id",announcement.getNoticeId());
            if(GBUtil.isBee()){
                 intent.putExtra("url","https://h5.bzz.dev/noticeDetail?app=leze&noticeId="+announcement.getNoticeId());
             }else{
                 intent.putExtra("url","https://h5.lezedata.com/noticeDetail?app=leze&noticeId="+announcement.getNoticeId());
             }


            baseVc().jumpActivityIntent(intent);

        }
    };
}
